package tigroids.timer;

import java.util.ArrayList;

import tigroids.timer.SingleTimer.AlarmDuration;

import android.app.Application;
import android.os.Messenger;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class TimerApplication extends Application {
	public static final String logAppId = "TigrasTimer";

	public static final String parameterNameCommand = "tigroids.timer.PN_CMD";
	public static final String parameterNameTimerToEdit = "tigroids.timer.PN_TTE";

	public static final int numberOfTimers = 4;

	ArrayList<SingleTimer> timers = new ArrayList<SingleTimer>();

	public Messenger messenger = null;
	private SharedPreferences persistentData;
	private static final String PERSISTENT_DATA_FILE = "PDF";
	public static final String TITLE_KEY = "title";
	public static final String MILLISECONDS_KEY = "millis";
	public static final String SOUND_KEY = "sound";
	public static final String ALARM_DURATION_KEY = "alarmduration";
	
	@Override
	public void onCreate() {
		super.onCreate();
		for (int cnt = 0; cnt < numberOfTimers; cnt++) {
			SingleTimer t = new SingleTimer(cnt, this);
			persistentData = getPersistentData();
			
			String title = null;
			long time = 0;
			switch(cnt) {
				case 0:
					title = "Tea";
					time = 3*60*1000;
					break;
				case 1:
					title = "Coffee";
					time = 4*60*1000;
					break;
				case 2:
					title = "Spaghetti";
					time = 8*60*1000;
					break;
				case 3:
					title = "Eggs";
					time = 150*1000;
					break;
			}
			
			t.setTitle(persistentData.getString(getKeyWithId(TITLE_KEY, cnt), title));
			t.setMilliseconds(persistentData.getLong(getKeyWithId(MILLISECONDS_KEY, cnt), time));
			t.setSoundOptionId(persistentData.getInt(getKeyWithId(SOUND_KEY, cnt), cnt));
			t.setDuration(AlarmDuration.getAlarmDurationById(persistentData.getInt(getKeyWithId(ALARM_DURATION_KEY, cnt), 1)));
			timers.add(t);
		}
	}

	public SharedPreferences getPersistentData() {
		return getSharedPreferences(TimerApplication.PERSISTENT_DATA_FILE,
				Context.MODE_PRIVATE);
	}

	/**
	 * Method generates kei for given key value and timer id in form of key_id
	 * 
	 * @param string
	 *            key value
	 * @param integer
	 *            timer id
	 * @return
	 */
	private String getKeyWithId(String titleKey, int i) {
		return titleKey.concat("-").concat(Integer.toString(i));
	}

	public SingleTimer getTimer(int cnt) {
		return timers.get(cnt);
	}

	public void saveTimer(SingleTimer timer) {
		Editor editor = persistentData.edit();
		editor.putString(getKeyWithId(TITLE_KEY, timer.getId()),
				timer.getTitle());
		editor.putLong(getKeyWithId(MILLISECONDS_KEY, timer.getId()),
				timer.getMilliseconds());
		editor.putInt(getKeyWithId(SOUND_KEY, timer.getId()),
				timer.getSoundOptionId());
		editor.putInt(getKeyWithId(ALARM_DURATION_KEY, timer.getId()),
				timer.getDuration().getId());
		editor.commit();
	}

}
